Ext.ns('AIR2.Project');
/***************
 * Project Organizations Panel
 */
AIR2.Project.Organizations = function() {
    return new AIR2.UI.Panel({
        storeData: AIR2.Project.ORGDATA,
        url: AIR2.Project.BASEURL + '/organizations',
        colspan: 1,
        title: 'Organizations',
        showTotal: true,
        iconCls: 'air2-project-organization-icon',
        itemSelector: '.air2-project-newsroom',
        tpl: new Ext.XTemplate(
            '<table><tpl for=".">' +
              '<tr class="air2-project-newsroom">' +
                '<td>{[AIR2.Format.orgName(values.Organization, true)]}</td>' +
                '<td>{[AIR2.Format.userName(values.ContactUser, true)]}</td>' +
                '<td><span class="phone">{[AIR2.Format.userPhone(values.ContactUser)]}</span></td>' +
                '<td>{[this.userEmail(values.ContactUser)]}</td>' +
              '</tr>' +
            '</tpl></table>',
            {
                compiled: true,
                disableFormats: true,
                userEmail: function(user) {
                    if (user.UserEmailAddress && user.UserEmailAddress.length > 0) {
                        var e = user.UserEmailAddress[0].uem_address;
                        return '<a class="email" href="mailto:'+e+'"></a>';
                    }
                    return '';
                }
            }
        ),
        editModal: {
            allowAdd: AIR2.Project.SUMMDATA.authz.may_manage,
            width: 650,
            items: {
                xtype: 'air2liveeditor',
                url: AIR2.Project.BASEURL + '/organizations',
                sortField: 'org_display_name',
                sortDir: 'ASC',
                pageSize: 50,
                allowEdit: AIR2.Project.SUMMDATA.authz.may_manage,
                allowDelete: AIR2.Project.SUMMDATA.authz.may_manage,
                hideButtons: true,
                editForm: {
                    layout:'column',
                    items: [{
                        xtype: 'container',
                        columnWidth: .5,
                        layout: 'form',
                        items: {
                            xtype: 'air2searchbox',
                            name: 'org_uuid',
                            fieldLabel: 'Organization',
                            searchUrl: AIR2.HOMEURL + '/organization',
                            baseParams: {
                                sort: 'org_display_name',
                                excl_proj: AIR2.Project.SUMMDATA.radix.prj_uuid
                            },
                            valueField: 'org_uuid',
                            displayField: 'org_display_name',
                            disabled: true,
                            msgTarget: 'under',
                            formatComboListItem: function(v) {
                                return v.org_display_name;
                            }
                        }
                    },{
                        xtype: 'container',
                        columnWidth: .5,
                        layout: 'form',
                        labelWidth: 95,
                        items: {
                            xtype: 'air2searchbox',
                            name: 'user_uuid',
                            fieldLabel: 'Primary Contact',
                            searchUrl: AIR2.HOMEURL + '/user',
                            valueField: 'user_uuid',
                            displayField: 'user_username',
                            msgTarget: 'under',
                            formatComboListItem: function(v) {
                                return AIR2.Format.userName(v.User);
                            },
                            listeners: {
                                beforequery: function(obj) {
                                    // get the org url
                                    var org = AIR2.HOMEURL+'/organization/'
                                    org += obj.combo.org_field.getValue();
                                    org += '/users.json';

                                    var r = Ext.data.Api.actions.read;
                                    obj.combo.store.proxy.setApi(r, org);
                                }
                            }
                        }
                    }],
                    listeners: {
                        beforeedit: function(form, rec) {
                            var f = form.getForm();
                            f.setValues(rec.data);

                            // setup org field
                            var orgfld = form.find('name', 'org_uuid')[0];
                            orgfld.setDisabled(!rec.phantom);
                            orgfld.setRawValue(rec.data.org_display_name);

                            // setup user field
                            var usrfld = form.find('name', 'user_uuid')[0];
                            usrfld.org_field = orgfld;
                            orgfld.setDisabled(!rec.phantom);
                            usrfld.setRawValue(rec.data.user_username);
                        },
                        beforesave: function(form, rec) {
                            var f = form.getForm();
                            f.updateRecord(rec);
                        }
                    }
                },
                tpl: new Ext.XTemplate(
                    '<div class="air2-project-newsroom">' +
                      '<div class="organization">' +
                        '<h3 class="fullname">{[AIR2.Format.orgNameLong(AIR2.unflattenCRUDObj(values,"Organization"), true)]}</h3>' +
                      '</div>' +
                      '<div class="contactuser">' +
                        '<span class="username">' +
                          '{[AIR2.Format.userName(values.ContactUser, true)]}' +
                        '</span>' +
                        '<span class="light">|</span>' +
                        '<span class="userphone">{[AIR2.Format.userPhone(values.ContactUser)]}</span>' +
                        '<span class="light">|</span>' +
                        '<span class="useremail">{[AIR2.Format.userEmail(values.ContactUser, true)]}</span>' +
                      '</div>' +
                      '<div class="meta">' +
                        '<span class="datewho">Added to project {[AIR2.Format.dateLong(values.porg_cre_dtim)]} ' +
                        'by {[AIR2.Format.userName(values.CreUser, true)]}</span>' +
                        '<button class="air2-rowedit"></button>' +
                        '<button class="air2-rowdelete"></button>' +
                      '</div>' +
                    '</div>',
                    {compiled: true, disableFormats: true}
                )
            }
        }
    })
}
